class Solution(object):
    def islandPerimeter(self, grid):
        """
        :type grid: List[List[int]]
        :rtype: int
        """
        m = len(grid)
        n = len(grid[0])
        count = 0
        for i in range(m):
            for j in range(n):
                if grid[i][j]:
                    for ux, uy in [(i - 1, j), (i + 1, j), (i, j + 1), (i, j - 1)]:
                        if 0 <= ux < m and 0 <= uy < n:
                            if not grid[ux][uy]:
                                count += 1
                        else:
                            count += 1
        return count


data = Solution()
grid = [[0, 1, 0, 0], [1, 1, 1, 0], [0, 1, 0, 0], [1, 1, 0, 0]]
print(data.islandPerimeter(grid))
